#ifndef IMAGEWIDGET_H
#define IMAGEWIDGET_H

#include <QtGui>

class ImageWidget : public QWidget
{
  Q_OBJECT

 public :
  ImageWidget(QWidget *parent=NULL);

  enum SliceType {
    DSlice,
    WSlice,
    HSlice
  };

  void saveImage();
  
  void setAreaTool(bool);

  void setGridSize(int, int, int);
  void setSliceType(int);
  void setImage(uchar*, uchar*);

  void setRawValue(QList<uchar>);

  void paintEvent(QPaintEvent*);
  void resizeEvent(QResizeEvent*);

  void keyPressEvent(QKeyEvent*);
  void mousePressEvent(QMouseEvent*);
  void mouseMoveEvent(QMouseEvent*);
  void mouseReleaseEvent(QMouseEvent*);
  void wheelEvent(QWheelEvent*);
  void enterEvent(QEvent*);
  void leaveEvent(QEvent*);

  void loadLookupTable(QImage);

  void depthUserRange(int&, int&);
  void widthUserRange(int&, int&);
  void heightUserRange(int&, int&);

 public slots :
  void updateTagColors();
  void sliceChanged(int);
  void userRangeChanged(int, int);

 signals :
  void setDeltaV(int);
  void setDeltaG(int);

  void getSlice(int);
  void getRawValue(int, int, int);
  void newMinMax(float, float);
  void saveProject();
  void tagDSlice(int, QImage);
  void tagWSlice(int, QImage);
  void tagHSlice(int, QImage);
  void tagDSlice(int, uchar*);
  void tagWSlice(int, uchar*);
  void tagHSlice(int, uchar*);
  void fillVolume(int, int,
		  int, int,
		  int, int,
		  QList<int>,
		  bool);
  void tagAllVisible(int, int,
		     int, int,
		     int, int);
  void dilate();
  void dilate(int, int,
	      int, int,
	      int, int);
  void erode();
  void erode(int, int,
	     int, int,
	     int, int);

 private :
  int m_currSlice;

  bool m_areaTool;

  int m_sliceType;
  int m_minDSlice, m_maxDSlice;
  int m_minWSlice, m_maxWSlice;
  int m_minHSlice, m_maxHSlice;
  int m_Depth, m_Width, m_Height;

  float m_zoom;

  int m_imgHeight, m_imgWidth;
  int m_simgHeight, m_simgWidth;
  int m_simgX, m_simgY;

  QImage m_image;
  QImage m_imageScaled;
  uchar *m_slice;
  uchar *m_sliceImage;

  QImage m_maskpad;
  QImage m_maskpadScaled;
  QImage m_mainusermask;
  QImage m_usermask;
  QImage m_usermaskScaled;
  QImage m_prevusermask;

  QImage m_maskimage;
  QImage m_maskimageScaled;
  uchar *m_maskslice;

  QImage m_difftagimage;
  QImage m_difftagimageScaled;

  uchar *m_lut;

  int m_button;
  bool m_pickPoint;
  int m_pickDepth, m_pickWidth, m_pickHeight;

  QList<uchar> m_vgt;
  QPoint m_cursorPos;

  QRectF m_rubberBand;
  bool m_rubberBandActive;
  bool m_rubberXmin;
  bool m_rubberYmin;
  bool m_rubberXmax;
  bool m_rubberYmax;
  bool m_rubberNew;

  bool m_automaticTagging;
  bool m_propagateTagging;
  uchar *m_prevtags;
  uchar *m_tags;
  bool m_propDir;
  int m_propSlice;

  bool m_copyMaskTags;

  QVector<QRgb> m_tagColors;
  
  void resizeImage();
  void recolorImage();
  
  void drawSizeText(QPainter*);
  void drawRawValue(QPainter*);
  void processCommands(QString);
  void drawRubberBand(QPainter*);

  bool checkRubberBand(int, int, bool);

  void updateRubberBand(int, int);
  bool validPickPoint(int, int);
  
  bool withinBounds(int, int);
  void calcImage(int, int);
  void dotImage(int, int);
  void removeDotImage(int, int);
  void addImage();
  void removeImage();

  void emitFill(bool);

  void setZoom(float);
  void preselect();

  void maskMask(QImage*);

  void updateUserMask();
  void tagSlice();

  void tagUntaggedRegion();
  void propagateTaggedRegion();
  void copyTaggedRegion();
  void checkPropagateTagging();

  void copyMaskTags(uchar*);
  void generateMaskImage();
  void calcDiffTags();
};


#endif
